﻿using DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UI.Models;

namespace UI.Controllers
{
    public class CategoryController : Controller
    {
        DaoRepository dao;
        public CategoryController()
        {
            this.dao = new DaoRepository();
        }

        public ActionResult Index(int? id)
        {
            ViewData["categories"] = dao.CategoryRepository.GetAll().ToList<Category>();
            if (id == 0 || !id.HasValue)
            {
                ViewData["cat_name"] = "Все товары";
                return View(dao.ProductRepository.GetAll());                
            }
            ViewData["cat_name"] = "Категория товаров: " + dao.CategoryRepository.GetById((int)id).Name;
            return View(dao.ProductRepository.GetAll().Where(t => t.Id_Category == id));
        }

        public ActionResult Details(int id)
        {
            return View(dao.ProductRepository.GetById(id));
        }

        public ActionResult DetailsForAll()
        {
            return View(ProductModel.GetObject().GetAll());
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Category/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Category/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Category/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Contacts()
        {
            return View();
        }
    }
}
